package IOperations;

import method.*;
import node.Node;

import java.util.Scanner;

public class Addition {
    public static void work() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入第一个整数");
        String num1 = scanner.nextLine();
        if(!isDataSafe.work(num1)) {
            return;
        }
        System.out.println("请输入第二个整数");
        String num2=scanner.nextLine();
        if(!isDataSafe.work(num2)) {
            return;
        }
        //判断是否为int范围内加法
        try {
            int i=Integer.parseInt(num1);
            int j=Integer.parseInt(num2);
            if(!isOverflow.sum(Integer.parseInt(num1),Integer.parseInt(num2))){
                System.out.println(Integer.parseInt(num1)+Integer.parseInt(num2));
                return;
            }
        } catch (Exception e) {

        }
        //创建双链表
        Node head1 = createNum.work(num1);
        Node head2 = createNum.work(num2);
        //如果为一正一负相加的情况，就将该情况传给相减，等于正数减（负数的绝对值）
        if(head1.val==0&&head2.val==1) {
            Subtraction.work(num2,num1.substring(1,num1.length()));
            return;
        } else if(head1.val==1&&head2.val==0) {
            Subtraction.work(num1,num2.substring(1,num2.length()));
            return;
        } else  if((head1.val==1&&head2.val==1) || (head1.val==0&&head2.val==0)){
            int firstNumLen = ListLength.work(head1);
            int secondNumLen = ListLength.work(head2);
            Node longHead,shortHead;
            if(firstNumLen==secondNumLen) {
                longHead=head1;
                shortHead=head2;
            } else {
                longHead = firstNumLen > secondNumLen ? head1 : head2;
                shortHead = firstNumLen < secondNumLen ? head1 : head2;
            }
            //开始做加法
            Node lonCur= toLastNode.work(longHead);
            Node shortCur=toLastNode.work(shortHead);
            while(shortCur!=shortHead) {
                int sum=lonCur.val+shortCur.val;
                if(sum<=9999) {
                    lonCur.val=sum;
                } else {
                    if(lonCur.prev==longHead) {
                        Node newNode = new Node(1);
                        headAdd.work(longHead,newNode);
                    } else {
                        lonCur.prev.val+=1;
                    }
                    lonCur.val=sum-10000;

                }
                shortCur=shortCur.prev;
                lonCur=lonCur.prev;
            }
            //完成计算打印最终值
            displayNum.work(longHead);
        }
    }
    public static void work(String num1,String num2) {
        //判断是否为int范围内加法
        try {
            int i=Integer.parseInt(num1);
            int j=Integer.parseInt(num2);
            if(!isOverflow.sum(Integer.parseInt(num1),Integer.parseInt(num2))){
                System.out.println(Integer.parseInt(num1)+Integer.parseInt(num2));
                return;
            }
        } catch (Exception e) {
        }
        //创建双链表
        Node head1 = createNum.work(num1);
        Node head2 = createNum.work(num2);
        int firstNumLen = ListLength.work(head1);
        int secondNumLen = ListLength.work(head2);
        Node longHead,shortHead;
        if(firstNumLen==secondNumLen) {
            longHead=head1;
            shortHead=head2;
        } else {
            longHead = firstNumLen > secondNumLen ? head1 : head2;
            shortHead = firstNumLen < secondNumLen ? head1 : head2;
        }
        //开始做加法
        Node lonCur= toLastNode.work(longHead);
        Node shortCur=toLastNode.work(shortHead);
        while(shortCur!=shortHead) {
            int sum=lonCur.val+shortCur.val;
            if(sum<=9999) {
                lonCur.val=sum;
            } else {
                if(lonCur.prev==longHead) {
                    Node newNode = new Node(1);
                    headAdd.work(longHead,newNode);
                }
                lonCur.val=sum-10000;
                lonCur.prev.val+=1;
            }
            shortCur=shortCur.prev;
            lonCur=lonCur.prev;
        }
        //完成计算打印最终值
        displayNum.work(longHead);
    }
}
